<?php
/**
 * DirectoryIterator.php - 29 Dec 2008
 * 
 * @author Kevin Breed
 */

class KB_DirectoryIterator extends FilterIterator
{
	/**
	 * @var int		Current iterate mode that corresponds with the I_MODE 
	 * 				constants
	 */
	protected $_iMode = 1;
	
	/**
	 * @var int		Iterate Mode. Iterate through files and directories, but  
	 * 				skip the dots
	 */
	const I_MODE_FILES_AND_DIRS = 1;
	
	/**
	 * @var int		Iterate Mode. Iterate through files only
	 */
	const I_MODE_FILES_ONLY = 2;
	
	/**
	 * @var int		Iterate Mode. Iterate through directories only
	 */
	const I_MODE_DIRS_ONLY = 3;
	
	/**
	 * @var int		Iterate Mode. Iterate through all stuff.
	 */
	const I_MODE_ALL = 4;
	
	##############
	# EXCEPTIONS #
	##############
	
	/**
	 * @var int		Exception code. Invalid iMode given.
	 */
	const E_INVALID_IMODE = 1;
	
	#######################
	# GETTERS AND SETTERS #
	#######################
	
	/**
	 * Set a new iterate mode. Possible modes are defined in the I_MODE class 
	 * values.
	 * 
	 * @param 	int		$mode	Indication for the iterate mode.
	 * @return	void
	 */
	public function setImode ( $mode )
	{
		if ( $mode > 4 || $mode < 1 )
		{
			throw new Exception( "'$mode' is an invalid iMode.", 
								 self::E_INVALID_IMODE );
			return;
		}
		$this->_iMode = $mode;
	}
	
	/**
	 * Returns the current iterator mode ($_iMode) as integer.
	 * 
	 * @return int	The current iterate mode.
	 */
	public function getImode ()
	{
		return $this->_iMode;
	}
	
	###################
	# MACHINE HANDLES #
	###################
	
	/**
	 * Construct.
	 */
	public function __construct ( $path )
	{
		parent::__construct( new DirectoryIterator( $path ) );
	}
	
	###########################
	# ITERATOR IMPLEMENTATION #
	###########################
	
	/** 
	 * Accept function
	 */
	public function accept ()
	{
		$item = $this->getInnerIterator();
		
		switch ( $this->getImode() )
		{
			case self::I_MODE_FILES_AND_DIRS :
			
				if ( !$item->isDot() && ( $item->isFile() || $item->isDir() ) )
					 return true;
				else return false;
			
			break;
			
			case self::I_MODE_FILES_ONLY :
			
				if ( !$item->isDot() && $item->isFile() )
					 return true;
				else return false;
			
			break;
			
			case self::I_MODE_DIRS_ONLY :
			
				if ( !$item->isDot() && $item->isDir() )
					 return true;
				else return false;
			
			break;
			
			case self::I_MODE_ALL :
			default :
				
				return true;
			
			break;
		}
	}
}

?>